<html>
    <head>
        <title>Login page</title>
        <link rel="stylesheet" href="css/boot.css"/>
    </head>
    <body>

    <center><div style='width:220px; padding:10px;border:3px solid lightgray;background-color:#EEEEFF;margin:15%;'>
            <form method="Post">
                <?php
                if (isset($_GET['err']))
                    echo "<div style='margin:5px; color:red;font-size:8pt;'>Wrong username or password</div>";

                require_once("classes/ldap.class.php");
                require_once("classes/db.class.php");
                include("mysql.config.php");
                include("ldap.config.php");
                ?>
                <div class="clearfix">
                    <div class="line">
                        <?php
                        echo "<select style='width:200px; padding:3px; position:relative; left:-2px;' name='lserv'>";
                        $i = 0;
                        foreach ($ldaps as $ldap) {
                            echo "<option value='$i'>" . $ldap['ldap_address'] . "</option>";
                            $i++;
                        }
                        echo "</select>";
                        ?>
                    </div>
                </div>
                <div class="clearfix">
                    <div class="line">
                        <input class="span4" style="height:30px;" type="text" name="username" placeholder="Username" required="required"/>
                    </div>
                </div>
                <div class="clearfix">
                    <div class="line">
                        <input class="span4" style="height:30px;" type="password" name="password" placeholder="Password" required="required"/>
                    </div>
                </div>
                <div class="clearfix">
                    <div class="line">
                        <input class="btn primary"  type="submit" name="ok" value="Login"/>
                    </div>
                </div>
            </form></div></center>

    <?php
    if (isset($_POST['ok'])) {
        $db = new DataBase($db_address, $db_username, $db_password, $db_db);
        $pass = md5($_POST['password']);
        $user = $_POST['username'];
        $db->select("`admins`", "*", "`name`='$user' AND `passw`='$pass'");

        if ($db->getCount() != 0) {
            session_start();
            session_register("uid");
            $_SESSION['uid'] = "admin";
            header("location:admin.php?p=home");
        } else {
            //__construct($host, $domain, $username=null, $password=null, $port="389")
            $a = new LDAP($ldaps[$_POST['lserv']]['ldap_address'], $ldaps[$_POST['lserv']]['ldap_users'], $_POST['username'], $_POST['password'], $ldaps[$_POST['lserv']]['ldap_port']);
            session_start();
            session_register("uid");
            $id = $a->getResult();
            $_SESSION['uid'] = $id;
            header("location:index.php");
        }
    }
    ?>
</body>
</html>